<?php

namespace app\common\service\pay\wechat;

use app\common\service\pay\PayInterface;
use think\facade\Log;

class JsApi extends WechatBase implements PayInterface
{

    public $trade_type = 'JSAPI';


    public function wechatFormat()
    {
        $data = parent::wechatFormat();

        $data['openid'] = $this->couponOrder->user->openid;
        return $data;
    }


    public function pay()
    {


        $formatData = $this->wechatFormat();

        $res = $this->app->order->unify($formatData);

        if (!isset($res['return_code']) || $res['return_code'] != 'SUCCESS') {
            Log::error($res['return_msg'] ?? '支付订单生成失败，请稍后再试。');
            return fail($res['return_msg'] ?? '支付订单生成失败，请稍后再试。');
        }


        $payment = $this->app->jssdk->bridgeConfig($res['prepay_id'], false);

        return $payment;

    }
}